//
//  KDEarlyWaringController.m
//  YuQingXiaoFang
//
//  Created by wangdongdong on 18/6/20.
//  Copyright © 2018年 KDTech. All rights reserved.
//

#import "KDEarlyWaringController.h"
#import "KDHomeViewController.h"
#import "KDEarlyWaringModel.h"
#import "KDEarlyWaringFrame.h"
#import "KDEarlyWaringCell.h"
#import "KDNavigationController.h"

#import "KDAutoMonitorController.h"
#import "KDsubscribController.h"
#import "KDMessagePushController.h"
@interface KDEarlyWaringController ()<UITableViewDelegate,UITableViewDataSource,KDEarlyWaringCellDelegate>
/**
 *  所有主题数据的集合
 */
@property (nonatomic, strong) NSArray * themeArr;
/**  Description */
@property (nonatomic, strong) NSArray *infoArr;
/**  <#Description#> */
@property (nonatomic, weak) UITableView *currentTabV;
/**
 *  加载第几页
 */
@property (nonatomic, copy) NSString *page;
@property (nonatomic,strong) NSArray *modelFrames;

@property (nonatomic,retain) NSDateFormatter *fmt;


@end
static CGFloat const tableViewH = 75;
@implementation KDEarlyWaringController

#pragma mark - 懒加载区

- (NSArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [NSArray array];
    }
    return _infoArr;
}
#pragma mark -生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    //[self setupNavBar];
    [self setUpTableView];
}

#pragma mark - 板块布置区
- (void)setUpTableView
{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , 0, screenW, screenH-20-navH-tabH) style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = 1000;
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    tableView.rowHeight = tableViewH;
    [tableView setSeparatorColor:[UIColor grayColor]];
    
    //1.调整(iOS8以上)tableView边距(与上面第2步等效,二选一即可)
    if ([tableView respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        tableView.preservesSuperviewLayoutMargins = NO;
    }
    //2.调整(iOS8以上)view边距
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if (!iOS7) {
        tableView.estimatedRowHeight = tableViewH;
    }
    self.currentTabV = tableView;
    [self.view addSubview:tableView];
    //UINib *xib =[UINib nibWithNibName:@"KDFocusListCell" bundle:nil] ;
    
    //[tableView registerNib:xib forCellReuseIdentifier:@"KDFocusListCell"];
    
    //tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDetailData)];
   // MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDetailData)];
   // [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    //self.currentTabV.footer = footer;
    //[self.currentTabV.header beginRefreshing];
    [self loadDetailData];
}
- (void)setupNavBar{
    //self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"退出" style:UIBarButtonItemStyleDone target:self action: @selector(loginoutSS:) ];
    
    //KDAccount *account = [KDAccountTool account];
    //NSString *time =[self.fmt stringFromDate:[[NSDate alloc] init]];
    //NSString *userNameAndTime=[NSString stringWithFormat:@"%@ %@",account.userName,time];
    //self.navigationItem.title=userNameAndTime;
    
}
#pragma mark - 事件点击区
- (void)leftItemClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 网络请求区
- (void)loadDetailData{
    self.infoArr = @[@"舆情预警", @"自主监测", @"预警推送", @"境外舆情",@"舆情搜索"];
    //创建frame模型对象
    NSMutableArray *modelFrameArray = [NSMutableArray array];
    for (NSInteger i=0; i<_infoArr.count; i++) {
        KDEarlyWaringFrame *modelFrame = [[KDEarlyWaringFrame alloc] init];
        KDEarlyWaringModel *model= [[KDEarlyWaringModel alloc ] init];
        model.title=self.infoArr[i];
        modelFrame.model = model;
        [modelFrameArray addObject:modelFrame];
    }
    //赋值
    self.modelFrames = modelFrameArray;
    [self.currentTabV.header endRefreshing];
    [self.currentTabV reloadData];
    
}
 //刷新数据
-(void) refreshData{
    [self.currentTabV.header beginRefreshing];
}
#pragma mark - 通知接收区
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDEarlyWaringCell *cell = [KDEarlyWaringCell cellWithTableView:tableView];
    //    KDLog(@"waimian%d", indexPath.row);
    cell.delegate = self;
    if (tableView == self.currentTabV)
    {
        cell.delegate = self;
        if (self.infoArr.count != 0)
        {
            cell.modelFrame = self.modelFrames[indexPath.row];
        }
        return cell;
    }else{
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    KDEarlyWaringFrame *modelFrame=self.modelFrames[indexPath.row];
    return modelFrame.cellHeight;
}

/**
 *  cell被点击了
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.currentTabV) {
        if (self.infoArr.count == 0) {
            return;
        }
        switch (indexPath.row) {
            case 0:{
                KDMessagePushController * ctr = [[KDMessagePushController alloc]init];
                [self presentToController:ctr title:self.infoArr[0]];
                break;
            }
            case 1:{
                KDAutoMonitorController * ctr = [[KDAutoMonitorController alloc]init];
                [self presentToController:ctr title:self.infoArr[1]];
                break;
            }
            case 2:{
                KDsubscribController * ctr = [[KDsubscribController alloc]init];
                [self presentToController:ctr title:self.infoArr[2]];
                break;
             }
            case 3:{
                
                break;
            }
            case 4:{
                
                break;
            }
             
            default:
                break;
        }
        
    }
}
- (void)presentToController:(UIViewController *)ctrl title:(NSString *)title{
    KDNavigationController *nav = [[KDNavigationController alloc] initWithRootViewController:ctrl];
    nav.navigationBar.topItem.title= title;
    // 推送到该控制器
    [self presentViewController:nav animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
